package com.ruoyi.evalution.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.evalution.mapper.LabEvalutionTopicMapper;
import com.ruoyi.evalution.mapper.LabEvalutionTopicTaskMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.evalution.domain.LabEvalutionTopic;
import com.ruoyi.evalution.domain.LabEvalutionTopicTask;
import com.ruoyi.evalution.service.ILabEvalutionTopicTaskService;

/**
 * 评测人员Service业务层处理
 * 
 * @author winner pan
 * @date 2022-08-03
 */
@Service
public class LabEvalutionTopicTaskServiceImpl implements ILabEvalutionTopicTaskService {
	@Autowired
	private LabEvalutionTopicTaskMapper labEvalutionTopicTaskMapper;
	@Autowired
	private LabEvalutionTopicMapper labEvalutionTopicMapper;
	/**
	 * 查询评测人员
	 * 
	 * @param evalutionTaskId
	 *            评测人员ID
	 * @return 评测人员
	 */
	@Override
	public LabEvalutionTopicTask selectLabEvalutionTopicTaskById(Long evalutionTaskId) {
		LabEvalutionTopicTask labEvalutionTopicTask = labEvalutionTopicTaskMapper.selectLabEvalutionTopicTaskById(evalutionTaskId);
		if(labEvalutionTopicTask.getScoreType()==2){
			//get parent
			LabEvalutionTopicTask query = new LabEvalutionTopicTask();
			query.setOrgId(labEvalutionTopicTask.getOrgId());
			query.setInstanceId(labEvalutionTopicTask.getInstanceId());
			query.setScoreType(1);
			List<LabEvalutionTopicTask> parentList = labEvalutionTopicTaskMapper.selectLabEvalutionTopicTaskList(query);
			if(parentList.size()==1){
				labEvalutionTopicTask.setParent(parentList.get(0));
			}else{
				throw new RuntimeException("实验室自评数据错误，请联系管理员核验数据库数据.");
			}
		}
		return labEvalutionTopicTask;
	}

	/**
	 * 查询评测人员列表
	 * 
	 * @param labEvalutionTopicTask
	 *            评测人员
	 * @return 评测人员
	 */
	@Override
	public List<LabEvalutionTopicTask> selectLabEvalutionTopicTaskList(LabEvalutionTopicTask labEvalutionTopicTask) {
		return labEvalutionTopicTaskMapper.selectLabEvalutionTopicTaskList(labEvalutionTopicTask);
	}

	/**
	 * 新增评测人员
	 * 
	 * @param labEvalutionTopicTask
	 *            评测人员
	 * @return 结果
	 */
	@Override
	public int insertLabEvalutionTopicTask(LabEvalutionTopicTask labEvalutionTopicTask) {
		return labEvalutionTopicTaskMapper.insertLabEvalutionTopicTask(labEvalutionTopicTask);
	}

	/**
	 * 修改评测人员
	 * 
	 * @param labEvalutionTopicTask
	 *            评测人员
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateLabEvalutionTopicTask(LabEvalutionTopicTask labEvalutionTopicTask) {
		if(labEvalutionTopicTask.getStatus()==1 && labEvalutionTopicTask.getScoreType()==1){
			LabEvalutionTopic topic = labEvalutionTopicMapper.selectLabEvalutionTopicById(labEvalutionTopicTask.getInstanceId());
			labEvalutionTopicTask.setSubmitTime(DateUtils.getNowDate());
			LabEvalutionTopicTask academyScore = new LabEvalutionTopicTask();
			academyScore.setOrgId(labEvalutionTopicTask.getOrgId());
			academyScore.setOrgName(labEvalutionTopicTask.getOrgName());
			academyScore.setScoreType(2);
			academyScore.setUserName(topic.getCreateBy());
			academyScore.setStatus(0);
			academyScore.setInstanceId(labEvalutionTopicTask.getInstanceId());
			academyScore.setCreateTime(labEvalutionTopicTask.getSubmitTime());
			labEvalutionTopicTaskMapper.insertLabEvalutionTopicTask(academyScore);
		}
		return labEvalutionTopicTaskMapper.updateLabEvalutionTopicTask(labEvalutionTopicTask);
	}

	/**
	 * 批量删除评测人员
	 * 
	 * @param evalutionTaskIds
	 *            需要删除的评测人员ID
	 * @return 结果
	 */
	@Override
	public int deleteLabEvalutionTopicTaskByIds(Long[] evalutionTaskIds) {
		return labEvalutionTopicTaskMapper.deleteLabEvalutionTopicTaskByIds(evalutionTaskIds);
	}

	/**
	 * 删除评测人员信息
	 * 
	 * @param evalutionTaskId
	 *            评测人员ID
	 * @return 结果
	 */
	@Override
	public int deleteLabEvalutionTopicTaskById(Long evalutionTaskId) {
		return labEvalutionTopicTaskMapper.deleteLabEvalutionTopicTaskById(evalutionTaskId);
	}
}
